import getProp from './prop'
import { formatAttribute } from './utils'
import { validateRequired } from '../rules/utils'

const TIPS_DATETIME = '选择'

/**
 * 属性转换 format_type
 * @param {*} { format_type } 需要转换的type
 * @return {*} type=YYYY-mm-> year-month
 */
function _formatType ({ format_type }) {
  // date time year-month month-day datehour
  // (("YYYY-mm", "年-月"),
  //         ("YYYY-mm-dd", "年-月-日"),
  //         ("YYYY-mm-dd HH:MM", "年-月-日 时:分"),
  //         ("HH:MM", "时:分")
  //         )
  const formatDate = [
    { type: 'YYYY-mm', format: 'year-month', field_format: 'YYYY-MM' },
    { type: 'YYYY-mm-dd HH:MM', format: 'datetime', field_format: 'YYYY-MM-DD HH:mm' },
    { type: 'YYYY-mm-dd', format: 'date', field_format: 'YYYY-MM-DD' },
    { type: 'HH:MM', format: 'time', field_format: 'HH-MM' },
  ]

  return formatAttribute(formatDate, format_type) || formatDate[0]
}

/**
 * 获取验证字段
 * @param {*} { fields }
 * @return {*}
 */
function _getRules ({ fields }) {
  const { label, required } = getProp({ fields })
  const { setting, verification } = fields
  const { format_type } = setting
  const { type } = verification
  const { format, field_format } = _formatType({ format_type })

  return {
    rules: [
      validateRequired(type, required, `请${TIPS_DATETIME}${label}！`),
    ],
    type: format,
    format: field_format,
  }
}

/**
 * @export 获取datetime类型表单属性
 * @param {*} { fields }
 * @return {*}
 */
export default function getDatetimeProp ({ fields }) {
  const rules = _getRules({ fields })

  return {
    ...getProp({ fields }),
    ...rules,
  }
}
